﻿namespace System.Web.Mvc
{
    using System.Collections.Specialized;
    using System.Web;

    public static class AjaxHelperCascadingDropDownExtensions
    {
        /// <summary>
        /// Registra el comportamiento de CascadingDropDown a un elemento SELECT.
        /// </summary>
        /// <param name="ajaxHelper">The extension method.</param>
        /// <param name="elementId">The ID of the extended SELECT element.</param>
        /// <param name="parentElementId">Optional ID of the parent DropDownList (upon which the contents of this control are based).</param>
        /// <param name="serviceMethod">Name of the web service method.</param>
        /// <param name="servicePath">Path to the helper web service.</param>
        /// <param name="category">Category of this DropDownList (used when communicating with the helper web service).</param>
        /// <param name="selectedValue">Selected value of the drop down.</param>
        /// <param name="loadingText">Optional text displayed by a DropDownList when it is loading its data.</param>
        /// <param name="prompText">Optional text displayed by a DropDownList the user has not yet touched. If omitted, first item in the dropdown is selected.</param>
        /// <param name="emptyText">Optional text for the option displayed when the list is empty.</param>
        public static void CascadingDropDown(this AjaxHelper ajaxHelper, string elementId, string parentElementId, string serviceMethod, string servicePath, string category, string selectedValue, string loadingText, string promptText, string emptyText)
        {
            ajaxHelper.RegisterScript("~/Scripts/AjaxControlToolkit/Common.js");
            ajaxHelper.RegisterScript("~/Scripts/AjaxControlToolkit/BaseScripts.js");
            ajaxHelper.RegisterScript("~/Scripts/AjaxControlToolkit/CascadingDropDownBehavior.js");

            // TODO: encode texts
            NameValueCollection options = new NameValueCollection();
            options.Add("id", elementId);
            if (!string.IsNullOrEmpty(parentElementId))
                options.Add("ParentControlID", parentElementId);
            if (!string.IsNullOrEmpty(serviceMethod))
                options.Add("ServiceMethod", serviceMethod);
            if (!string.IsNullOrEmpty(servicePath))
                options.Add("ServicePath", VirtualPathUtility.ToAbsolute(servicePath, ajaxHelper.ViewContext.HttpContext.Request.ApplicationPath));
            if (!string.IsNullOrEmpty(category))
                options.Add("Category", category);
            if (!string.IsNullOrEmpty(selectedValue))
                options.Add("SelectedValue", selectedValue);
            if (!string.IsNullOrEmpty(loadingText))
                options.Add("LoadingText", loadingText);
            if (!string.IsNullOrEmpty(promptText))
                options.Add("PromptText", promptText);
            if (!string.IsNullOrEmpty(emptyText))
                options.Add("EmptyText", emptyText);

            ajaxHelper.RegisterScriptBehavior(elementId, "AjaxControlToolkit.CascadingDropDownBehavior", options);
        }
    }
}
